package com.lanlian.cayman.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.lanlian.commonlib.bean.UploadDataInfo;
import com.lanlian.commonlib.common.SystemConfig;
import com.lanlian.cayman.R;
import com.lanlian.commonlib.dao.DBManager;
import com.lanlian.imagelib.ShowImageUtils;
import com.lanlian.uikit.CustomDialog;
import com.lanlian.utillib.ViewFindUtils;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/11/6 17:29
 *     desc   : 上传材料适配器
 *     version: 1.0
 * </pre>
 */
public class UploadAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<String> mMediaUrlList;
    private ArrayList<UploadDataInfo> mUploadDataList;

    public UploadAdapter(Context context, ArrayList<String> list) {
        this.mContext = context;
        this.mMediaUrlList = list;
    }

    public UploadAdapter(ArrayList<UploadDataInfo> list, Context context) {
        this.mContext = context;
        this.mUploadDataList = list;
    }

    @Override
    public int getCount() {
        if (mUploadDataList != null) {
            return mUploadDataList.size();
        }
        return mMediaUrlList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (mUploadDataList != null) {
            return 0;
        }
        if (position < mMediaUrlList.size()) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_upload, null);
        }
        ImageView image = ViewFindUtils.hold(convertView, R.id.image);
        ImageView imgDelete = ViewFindUtils.hold(convertView, R.id.img_delete);

        switch (getItemViewType(position)) {
            case 0:
                if (mUploadDataList != null) {
                    ShowImageUtils.showRoundImage(mContext, SystemConfig.IMAGE_BASE_URL + mUploadDataList.get(position).getFileUrl(),
                            5, SystemConfig.IMG_DEFAULT_ABLUM, image);
                    imgDelete.setVisibility(View.GONE);
                } else {
                    final String imageUrl = mMediaUrlList.get(position);
                    String url = String.format("file://%s", imageUrl);
                    ShowImageUtils.showRoundImage(mContext, url, 5, SystemConfig.IMG_DEFAULT_ABLUM, image);
                    imgDelete.setVisibility(View.VISIBLE);
                }
                break;
            case 1:
                imgDelete.setVisibility(View.GONE);
                if (DBManager.getInstance().getTopicColor() == 1) {
                    ShowImageUtils.showRoundImage(mContext, R.mipmap.uploaddata_icon_addto, 5,
                            SystemConfig.IMG_DEFAULT_BACKGROUND, image);
                } else {
                    ShowImageUtils.showRoundImage(mContext, R.mipmap.uploaddata_icon_addto_night, 5,
                            SystemConfig.IMG_DEFAULT_BACKGROUND, image);
                }
                break;
            default:
                break;
        }

        imgDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new CustomDialog(mContext, new CustomDialog.OnClickListener() {
                    @Override
                    public void onLeftClick() {
                    }

                    @Override
                    public void onRightClick() {
                        mMediaUrlList.remove(position);
                        notifyDataSetChanged();
                    }
                }).setTitle("确定删除吗？").show();
            }
        });

        return convertView;
    }

}
